import app from '../../bin/server'
import supertest from 'supertest'
import { expect, should } from 'chai'
import { cleanDb, authTestUser } from '../utils'

should()
const request = supertest.agent(app.listen())
const context = {}

describe('Terms', () => {
  before((done) => {
    // cleanDb()
    console.log('在进行测试前，通过测试账号获取授权')
    authTestUser(request, (err, { user, token }) => {
      if (err) { return done(err) }
      console.log('获取授权成功')
      context.user = user
      context.token = token
      done()
    })
  })
  describe('POST /terms', () => {
        it('成功创建一期答题', (done) => {
            const { token } = context
          request
            .post('/terms')
            .set({
              Accept: 'application/json',
              Authorization: `Bearer ${token}`
            })
            .send({ term: { terms_name: 'term2', terms_subtitle: 'this is a  terms_subtitle' } })
            .expect(200, (err, res) => {
              if (err) { return done(err) }
              res.body.should.have.property('terms')
              done()
            })
        })

        it('由于标题为空，创建答题失败', (done) => {
            const { token } = context
          request
            .post('/terms')
            .set({
              Accept: 'application/json',
              Authorization: `Bearer ${token}`
            })
            .send({ term: { terms_name: '', terms_subtitle: 'this is a  terms_subtitle' } })
            .expect(422, (err, res) => {
              if (err) { return done(err) }
              done()
            })
        })

        it('获取答题期数列表', (done) => {
            const { token } = context
          request
            .get('/terms')
            .set({
              Accept: 'application/json',
              Authorization: `Bearer ${token}`
            })
            .send({ term: { terms_name: '', terms_subtitle: 'this is a  terms_subtitle' } })
            .expect(200, (err, res) => {
              if (err) { return done(err) }
              // console.log(res.body)
              res.body.should.have.property('terms')
              done()
            })
        })

        it('通过存在的Id获取某一期答题', (done) => {
            const {
              user: { _id },
              token
            } = context
          request
            .get(`/terms/4`)
            .set({
              Accept: 'application/json',
              Authorization: `Bearer ${token}`
            })
            .expect(200, (err, res) => {
              if (err) { return done(err) }
              // console.log(res.body)
              res.body.should.have.property('terms')
              done()
            })
        })

        it('通过不存在的Id获取某一期答题', (done) => {
            const {
              user: { _id },
              token
            } = context
          request
            .get(`/terms/57fca8847bf47bs23501c0d4f`)
            .set({
              Accept: 'application/json',
              Authorization: `Bearer ${token}`
            })
            .expect(404,done)
        })
  })

})
